//
//  MainViewController.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/2.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class MainViewController: BaseViewController,helpBannerTurn {

    let banner = CycleBanner.init(frame: CGRect.init(x: 0, y: 87 * YXHeightScale, width: YXDeviceWidth, height: 300 * YXHeightScale))
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "首页"
        self.view.addSubview(banner)
        banner.delegate = self
        banner.autoCycle(interval: 5.0)
        self.automaticallyAdjustsScrollViewInsets = false
        let name = DataCacher.shared().name()
        let address = self.addressView(address: name)
        self.view.addSubview(address)
        self.view.backgroundColor = UIColor.colorWithHexString(hex: "fefdfb")
        self.configRemainingView()
        self.automaticallyAdjustsScrollViewInsets = false
        self.loadBanner()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    private func addressView(address : String)->UIView{
        let tray = UIView.init()
        tray.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: YXDeviceWidth, height: 88 * YXHeightScale))
        tray.backgroundColor = UIColor.colorWithHexString(hex: "fcfcfc")
        let imageView = UIImageView.init()
        imageView.frame = CGRect.init(x: 28 * YXWidthScale, y: 26 * YXHeightScale, width: 26 * YXWidthScale, height: 34 * YXHeightScale)
        let image = UIImage.load(name: "icon_home_add")
        imageView.image = image
        tray.addSubview(imageView)
        let label = UILabel.init()
        let font = UIFont.systemFont(ofSize: 32 * YXWidthScale)
        let size = address.StringSize(font: font)
        label.frame = CGRect.init(x: imageView.right()+20*YXWidthScale, y: imageView.top(), width: size.width, height: size.height)
        label.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        label.text = address
        label.font = font
        tray.addSubview(label)
        return tray
    }
    
    func configRemainingView() -> Void {
        let imageView = UIImageView.init()
        imageView.frame = CGRect.init(x: 70 * YXHeightScale, y: 56 * YXWidthScale + banner.bottom(), width: 130 * YXWidthScale, height: 260 * YXHeightScale)
        let image = UIImage.load(name: "pic_home_iphone")
        imageView.image = image
        self.view.addSubview(imageView)
        
        let label = UILabel.init()
        let font = UIFont.boldSystemFont(ofSize: 32 * YXWidthScale)
        label.text = "用户下载估价软件(Download)"
        let size = label.text!.StringSize(font: font)
        label.frame = CGRect.init(x: imageView.right() + 42 * YXWidthScale, y: banner.bottom() + 92 * YXHeightScale, width: size.width, height: size.height)
        label.font = font
        label.textColor = UIColor.colorWithHexString(hex: "2c2c2c")
        self.view.addSubview(label)
        let dot1 = self.dotViewWith(origin: CGPoint.init(x: imageView.right() + 42 * YXWidthScale, y: label.bottom() + 42 * YXHeightScale), title: "检测硬件性能给出估价范围")
        self.view.addSubview(dot1)
        let dot2 = self.dotViewWith(origin: CGPoint.init(x: dot1.left(), y: dot1.bottom() + 26 * YXHeightScale), title: "扫码获取信息")
        self.view.addSubview(dot2)
        let dot3 = self.dotViewWith(origin: CGPoint.init(x: dot1.left(), y: dot2.bottom() + 26 * YXHeightScale), title: "营业厅检测外观给出具体价格")
        self.view.addSubview(dot3)
        
        let button = UIButton.init(type: .system)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 36 * YXWidthScale)
        button.setTitle("扫二维码", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        let image2 = UIImage.load(name: "icon_scan")
        let image3 = image2?.compress(size: CGSize.init(width: 30, height: 30))
        button.setImage(image3, for: .normal);
        button.backgroundColor = UIColor.black
        button.layer.cornerRadius = YXCornerRadius
        button.frame = CGRect.init(x: YXDeviceWidth / 2 - 330 * YXWidthScale, y: imageView.bottom() + 76 * YXHeightScale, width: 660 * YXWidthScale, height: 108 * YXHeightScale)
        button.tintColor = UIColor.white
        self.view.addSubview(button)
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0)
        button.addTarget(self, action: #selector(scan(btn:)), for: .touchUpInside)
    }
    
    @objc func scan(btn : UIButton) -> Void {
//        let bin = BinaryInfoViewController.init()
//        bin.hidesBottomBarWhenPushed = true
//        bin.url = "https://www.elerecycle.com/erecycle/productdetection/findProductDetection.do?id=552"
//        self.navigationController?.pushViewController(bin, animated: true)
        let scan = ScanBinaryCodeViewController.init()
        scan.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(scan, animated: true)
    }
    
    func dotViewWith(origin : CGPoint,title : String) -> UIView {
        let tray = UIView.init()
        let font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        let size = title.StringSize(font: font)
        let image = UIImage.load(name: "black_c")
        let imageView = UIImageView.init()
        imageView.frame = CGRect.init(x: 0, y: size.height / 2 - 5 * YXHeightScale, width: 10 * YXHeightScale, height: 10 * YXWidthScale)
        imageView.image = image
        tray.addSubview(imageView)
        let label = UILabel.init()
        label.frame = CGRect.init(x: imageView.right() + 18 * YXWidthScale, y: 0, width: size.width, height: size.height)
        label.textColor = UIColor.colorWithHexString(hex: "646464")
        label.font = UIFont.systemFont(ofSize: 28 * YXWidthScale)
        label.textAlignment = .left
        label.text = title
        tray.addSubview(label)
        tray.frame = CGRect.init(origin: origin, size: CGSize.init(width: 28 * YXWidthScale, height: size.height))
        return tray
    }
    
    func loadBanner() -> Void {
        self.beginAnim()
        Manager.shared().bannerInfo(success: { (model) in
            self.endAnim()
            if model.result == true {
                let bannerMod = model as! BannerModel
                self.banner.source = bannerMod.itemModels
            } else {
                self.commonAlertMsg(content: model.msg)
            }
        }, fail: {(code) in
            self.endAnim()
            self.commonAlertMsg(content: YXNetWorkFail)
        })
    }
    func turn(url : String) -> Void{
        let ban = BannerContentViewController.init()
        ban.hidesBottomBarWhenPushed = true
        ban.url = url
        self.navigationController?.pushViewController(ban, animated: true)
    }
}
